#include "cd.h"

void cd(int peerfd, char* buf) {

    char pathnameNow[1024];
    getcwd(pathnameNow, sizeof(pathnameNow));

    int retval1 = chdir(buf);
    if(retval1 == -1) {
        sendMsg(peerfd, errcd);
        return;
    }

    char checkpath[1024];
    getcwd(checkpath, sizeof(checkpath));
    char empty[28] = {0};
    strncat(empty, checkpath, 28);
    int retval2 = strcmp(empty, dir);
    if(retval2 != 0) {
        sendMsg(peerfd, errcd);
        chdir(pathnameNow);
        return;
    }

    char *pathname = getwd(buf);

    train_t sendmsgs;
    sendmsgs.len = strlen(pathname) + 1; //先传返回信息的大小
	strcpy(sendmsgs.data, pathname); //再传返回的信息
	send(peerfd, &sendmsgs, sendmsgs.len + 4, MSG_WAITALL);
}
